﻿using AutoMapper;

using BCVP.Net8.Common;
using BCVP.Net8.IService;
using BCVP.Net8.Model.Models;
using BCVP.Net8.Model.ViwModels;
using BCVP.Net8.Repository;
using BCVP.Net8.Repository.Base;

namespace BCVP.Net8.Service
{
    /// <summary>
    /// 用户服务接口实现
    /// </summary>
    public class UserService : BaseService<SysUserInfo, SysUserInfoViewModel>, IUserService
    {
        private readonly IDepartmentServices _departmentServices;
        private readonly IBaseRepository<SysUserInfo> _dal;

        private readonly IAccessTrendLogService _accessTrendLogService;

        public UserService(IBaseRepository<SysUserInfo> baseRepository, IMapper mapper, IDepartmentServices departmentServices, IAccessTrendLogService accessTrendLogService) : base(baseRepository, mapper)
        {
            _departmentServices = departmentServices;
            _dal = baseRepository;
            _accessTrendLogService = accessTrendLogService;
        }


        /// <summary>
        /// 测试使用同事务
        /// </summary>
        /// <returns></returns>
        [UseTran(Propagation = Propagation.Required)]
        public async Task<bool> TestTranPropagation()
        {
            await Console.Out.WriteLineAsync($"db context id : {base.Db.ContextID}");
            var sysUserInfos = await base.QueryAll();

            TimeSpan timeSpan = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var id = timeSpan.TotalSeconds.ObjToLong();
            var insertSysUserInfo = await _dal.Add(new SysUserInfo()
            {
                Id = id,
                Name = $"user name {id}",
                Status = 0,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                CriticalModifyTime = DateTime.Now,
                LastErrorTime = DateTime.Now,
                ErrorCount = 0,
                Enable = true,
                TenantId = 0,
            });
            //var model = await Add(new SysUserInfo()
            //{
            //    Id = id,
            //    Name = $"user name {id}",
            //    Status = 0,
            //    CreateTime = DateTime.Now,
            //    UpdateTime = DateTime.Now,
            //    CriticalModifyTime = DateTime.Now,
            //    LastErrorTime = DateTime.Now,
            //    ErrorCount = 0,
            //    Enable = true,
            //    TenantId = 0,
            //});

            await _accessTrendLogService.QueryByName("用户信息");

            await _departmentServices.TestTranPropagation2();

            return true;
        }
    }
}
